<!--
* @author: JT
* @Date: 2024-08-02
* @description: PC端
* @Last Modified time: 2024-08-02 14:52:31
-->
<template>
  <div class="homePage">
    <SuScrollbar height="100vh" @scroll="scrolling">
      <div class="page-main">
        <header>
          <div class="head-mask" :style="{ opacity: state.opacity }"></div>
          <div class="head-logo">
            <img :src="state.appLogo" alt="logo" />
          </div>
          <div class="head-user">
            <div class="info-item" title="所在科室">
              <i class="fa fa-org" />
              <span>{{ state.departmentName }}</span>
            </div>
            <div class="info-item" title="用户名">
              <div class="dropdownBox">
                <div class="dropdown-btn">
                  <i class="fa fa-user-web" />
                  <span class="info-text">{{ state.userName }}</span>
                  <i class="fa fa-angle-down" />
                </div>
                <div class="dropdown-panel">
                  <div class="panel-box">
                    <a
                      class="fa fa-password size15 sideTxt"
                      title="修改密码"
                      @click="editPassword"
                    >
                      修改密码</a
                    >
                    <a
                      class="fa fa-power size15 sideTxt"
                      title="退出系统"
                      @click="logoutHandle"
                    >
                      退出系统</a
                    >
                  </div>
                </div>
              </div>
            </div>
          </div>
        </header>
        <main>
          <section>
            <div class="head-home">
              <div class="home-name">{{ state.appName }}</div>
              <div class="home-remark">{{ state.appRemark }}</div>
            </div>
            <img class="head-img" :src="homeHeadImg" />
          </section>
          <div class="app-wrap">
            <template v-if="state.list.length">
              <div
                class="menu-app"
                v-for="(row, index) in state.list"
                :key="index"
              >
                <div
                  v-for="item in row"
                  :key="item.id"
                  class="app-item"
                  :title="item.remark || ''"
                  @click="jumpToApp(item.id)"
                >
                  <Dropdown :trigger="['contextmenu']">
                    <div class="item-inner">
                      <img
                        class="app-icon"
                        :src="getImgSrc(`portal-app${item.portalImg || 1}.png`)"
                        alt=""
                      />
                      <div class="item-info">
                        <p class="app-name" v-html="item.name"></p>
                        <p class="app-tip">进入系统 >></p>
                      </div>
                      <!-- <div v-if="item.remark" class="app-remark">
												<p>{{ item.remark }}</p>
											</div> -->
                    </div>
                    <!-- 右键菜单 -->
                    <template #overlay>
                      <Menu>
                        <MenuItem key="1">
                          <div @click="jumpToApp(item.id, '_blank')">
                            <p class="size12">
                              在新窗口打开项目：《{{ repalceName(item) }}》
                            </p>
                            <p class="showLine">{{ item.homePage }}</p>
                          </div>
                        </MenuItem>
                      </Menu>
                    </template>
                  </Dropdown>
                </div>
              </div>
            </template>
            <Empty
              v-else
              class="paT30 maT30 grayTxt"
              description="该用户未授予项目应用"
            ></Empty>
          </div>
        </main>
      </div>
    </SuScrollbar>
  </div>
  <PasswordModal
    v-model:show="state.showPassword"
    :name="state.userName"
    closable
    @callback="logOut"
  />
</template>

<script setup lang="ts">
import { traceCustomEvent } from '@web-tracing/vue3';
import SuScrollbar from 'components/src/su-scrollbar/index.vue';
import PasswordModal from './passwordModal.vue';
import { reactive, onMounted, createVNode } from 'vue';
import {
  Modal,
  notification,
  MenuItem,
  Empty,
  Dropdown,
  Menu,
} from 'ant-design-vue';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import loginApi from 'apis/src/portal/login';
import homeApi from 'apis/src/portal/home';
import portalApi, { IUserInfo } from 'apis/src/portal/common';
import { IAppItem } from 'apis/src/portal/home';
import homeHeadImg from '../../assets/imgs/home-head-img.png';
import { getAssetsFile } from 'commons/src/imgUtil';

const getImgSrc = (url: string) => {
  const src = new URL(`../../assets/imgs/app-icon/${url}`, import.meta.url);
  return src.href;
};

const state = reactive({
  appLogo: '', // 项目logo
  appName: '', // 项目名称
  appRemark: '', // 备注
  version: '', // 项目版本号
  departmentName: '', // 所属科室
  userName: '', // 用户名
  opacity: 0,
  list: [] as IAppItem[][],
  showPassword: false,
  roles: [] as IUserInfo['roles'],
});
onMounted(() => {
  getAppInfo();
  getUserInfo();
  getList();
});
const repalceName = (item: IAppItem) => {
  return item?.name.replace(/<br \/>/gm, '');
};

// 获取系统信息
const getAppInfo = () => {
  loginApi.getPortalInfo().then((res) => {
    state.appLogo = res.data.logUrl || getAssetsFile('app-logo.svg');
    state.appName = res.data.name || '医疗质量监控管理平台';
    state.appRemark =
      res.data.remark ||
      '基于大数据交换处理技术，为医疗质量管理决策提供准确的数据支撑。';
    state.version = res.data.version || 'v1.0.1';
  });
};
// 获取用户信息
const getUserInfo = () => {
  const appCode = import.meta.env.VITE_BASE_PORTAL_CODE;
  portalApi.getUserInfo(appCode).then((res) => {
    const { departmentName, code, roles } = res.data;
    state.departmentName = departmentName;
    state.userName = code;
    state.roles = roles;
  });
};
// 修改密码
const editPassword = () => {
  state.showPassword = true;
};
// 退出系统
const logoutHandle = () => {
  Modal.confirm({
    title: '确定要退出系统吗？',
    icon: createVNode(ExclamationCircleOutlined),
    content: createVNode(
      'div',
      {
        style: 'color:red;',
      },
      '退出后若要操作，需要重新登录'
    ),
    okText: '确定',
    cancelText: '取消',
    onOk() {
      logOut();
    },
  });
};
// ajax退出登录
const logOut = () => {
  loginApi.logOut().then(() => {
    // 埋点
    traceCustomEvent({ title: '用户退出', customType: 'logout' });
    localStorage.removeItem('token');
    localStorage.removeItem('isPwdExpired');
    localStorage.removeItem('encryp');
    location.reload();
  });
};
// 获取列表
const getList = () => {
  homeApi.getList().then((res) => {
    state.list = res.data;
  });
};
// 滚动
const scrolling = ({ y }: { y: number }) => {
  state.opacity = y > 30 ? 1 : 0;
};
// 跳转系统
const jumpToApp = (id: string, _blank?: string) => {
  const arr = state.list.flat();
  const match = arr.find((V) => V.id === id);
  if (match && match.homePage) {
    const token = localStorage.getItem('token');
    const fix = match.homePage.includes('?') ? '&' : '?';
    let url;
    if (match.thirdPartySystem === 'Y') {
      // 第三方系统或是旧系统
      url =
        match.homePage +
        fix +
        `tpsCiphertext=${encodeURIComponent(
          match.tpsCiphertext || ''
        )}&tsp=${encodeURIComponent(window.location.href)}`;
    } else {
      url = match.homePage + fix + `token=${token}`;
    }

    // 埋点
    traceCustomEvent({ title: '进入子系统', customType: 'click' });
    if (_blank) {
      // 新窗口打开链接
      window.open(url, _blank);
    } else {
      window.location.href = url;
    }
  } else {
    notification.error({
      message: '错误警告：',
      description: '无匹配的跳转路径，请联系管理员！',
      duration: 10,
    });
  }
};
</script>
<style lang="scss" scoped>
@import 'commons/src/css/scss/variate.scss';

.homePage {
  width: 100%;
  height: 100%;
  background-color: #f9fafb;
  position: relative;
  min-width: 1200px;
  overflow-y: hidden;
}

$headHeight: 76px;
$marginLR: 15%; // 左右边距

.page-main {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: url('../../assets/imgs/home-head.png') no-repeat;
  background-size: 100%;

  header {
    width: calc(100vw - 0px);
    height: $headHeight;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: fixed;
    z-index: 1000;

    &:hover {
      .head-mask {
        opacity: 1;
      }
    }

    .head-mask {
      width: 100%;
      height: 100%;
      position: absolute;
      background: #f9fafb;
      box-shadow: 0px 6px 58px 0px rgba(196, 203, 214, 0.2);
      border-bottom: 1px solid #fff;
      transition: all 0.3s;
      opacity: 0;
    }

    .head-logo {
      // width: 184px; // 2024.10.22宽度自适应，只设置logo高度
      height: 44px;
      margin-left: $marginLR;
      display: flex;
      justify-content: flex-start;
      // align-items: center;
      overflow: hidden;
      position: relative;
      z-index: 11;
    }

    .head-user {
      margin-right: $marginLR;
      color: $theme-side-text-color;
      display: inline-flex;
      align-items: center;
      position: relative;
      z-index: 11;

      .info-item {
        font-size: 15px;
        padding: 0 16px;
      }

      i.fa {
        padding: 0 3px;
      }
    }
  }

  section {
    padding: $headHeight $marginLR 0;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .home-name {
      font-size: 34px;
      letter-spacing: 2px;
      line-height: 60px;
    }

    .head-img {
      width: 26%;
      max-width: 340px;
      margin-left: 10%;
    }
  }

  .app-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 10px 0;
  }

  .menu-app {
    width: 1090px;
    display: flex;
    flex-wrap: wrap;

    .app-item {
      margin: 16px 26px;
      width: 220px;
      height: 150px;
      background-color: #f4f6f8;
      border-radius: 24px;
      box-shadow: -6px -8px 12px 0 rgba(255, 255, 255, 0.98);
      cursor: pointer;
      transition: all 0.3s;

      &:hover {
        box-shadow: 4px 6px 16px 0 rgb(65 124 233 / 18%) inset;

        .item-inner {
          box-shadow: 5px 6px 16px 0 rgb(30 111 255 / 14%);
          border-color: $blue-color;

          .item-info {
            .app-name,
            .app-tip {
              color: $blue-color;
            }
          }
        }
      }
    }
    .item-inner {
      width: 100%;
      height: 100%;
      padding: 15px;
      border: 1px solid #fff;
      border-radius: 24px;
      box-shadow: 5px 6px 16px 0px rgba(65, 124, 233, 0.14);
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      transition: all 0.3s;
      position: relative;
      overflow: hidden;

      .app-icon {
        display: block;
        width: 50px;
        height: 50px;
        flex-shrink: 0;
      }

      .item-info {
        flex: 1;
        padding-top: 10px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;

        .app-name {
          color: #2e4758;
          font-size: 16px;
          line-height: 20px;
          transition: all 0.3s;
        }
        .app-tip {
          color: #91929e;
          font-size: 12px;
          transition: all 0.3s;
        }
      }

      .app-remark {
        width: 100%;
        height: 100%;
        padding: 20px;
        color: #fff;
        background-color: rgba(0, 0, 0, 0.4);
        display: flex;
        justify-content: center;
        align-items: center;
        position: absolute;
        left: 0;
        top: 100%;
        transition: all 0.3s;
      }

      &:hover {
        .app-remark {
          top: 0%;
        }
      }
    }
  }

  @media screen and (max-width: 1280px) {
    section {
      .home-name {
        font-size: 30px;
      }
    }

    .menu-app {
      width: 890px;
      .app-item {
        margin: 16px 16px;
        width: 190px;
        height: 140px;
        border-radius: 18px;
      }
      .item-inner {
        padding: 10px 12px;
        border-radius: 18px;
        .app-icon {
          width: 46px;
          height: 46px;
        }
        .item-info {
          .app-name {
            font-size: 15px;
            line-height: 18px;
          }
        }
      }
    }
  }
  @media screen and (min-width: 1281px) and (max-width: 1440px) {
    section {
      .home-name {
        font-size: 32px;
      }
    }

    .menu-app {
      width: 1090px;
      .app-item {
        margin: 16px 26px;
        width: 220px;
        height: 150px;
        border-radius: 20px;
      }
      .item-inner {
        padding: 10px 16px;
        border-radius: 20px;
        .app-icon {
          width: 50px;
          height: 50px;
        }
        .item-info {
          .app-name {
            font-size: 16px;
            line-height: 18px;
          }
        }
      }
    }
  }
  @media screen and (min-width: 1441px) and (max-width: 1680px) {
    section {
      .home-name {
        font-size: 32px;
      }
    }

    .menu-app {
      width: 1210px;
      .app-item {
        margin: 16px 26px;
        width: 250px;
        height: 160px;
        border-radius: 24px;
      }
      .item-inner {
        padding: 15px 18px;
        border-radius: 24px;
        .app-icon {
          width: 56px;
          height: 56px;
        }
        .item-info {
          .app-name {
            font-size: 16px;
            line-height: 20px;
          }
        }
      }
    }
  }
  @media screen and (min-width: 1681px) and (max-width: 1920px) {
    .menu-app {
      width: 1250px;
      .app-item {
        margin: 16px 26px;
        width: 260px;
        height: 170px;
        border-radius: 26px;
      }
      .item-inner {
        padding: 16px 18px;
        border-radius: 24px;
        .app-icon {
          width: 60px;
          height: 60px;
        }
        .item-info {
          .app-name {
            font-size: 17px;
            line-height: 20px;
          }
        }
      }
    }
  }
  @media screen and (min-width: 1921px) {
    .menu-app {
      .app-item {
        margin: 16px 26px;
        width: 270px;
        height: 180px;
        border-radius: 26px;
      }
      .item-inner {
        padding: 16px 18px;
        border-radius: 24px;
        .app-icon {
          width: 60px;
          height: 60px;
        }
        .item-info {
          .app-name {
            font-size: 17px;
            line-height: 20px;
          }
        }
      }
    }
  }
}
</style>
